﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using IGM.DataProvider;
using IGM.Framework.Commons;
using IGM.Framework.Constants;

namespace IGM.Presentation.Management
{
    public partial class OtherControl : UserControl
    {
        private readonly Business.Business _business;

        public OtherControl()
        {
            InitializeComponent();
            _business = Main.Main.Instance.IgmBusiness;
        }

        private void OtherControlLoad(object sender, EventArgs e)
        {
            LoadMajor();
            LoadKindIntern();
            LoadKindMark();
            Dock = DockStyle.Top;
        }

        #region -- Major --

        /// <summary>
        /// 
        /// </summary>
        private void LoadMajor()
        {
            dgvMajors.DataSource = _business.GetListMajor();

            DataGridViewHelper.ShowHideColumns(dgvMajors,
                                               new[] { PropertiesConstants.IsChecked, PropertiesConstants.Name, "Code"});
            DataGridViewHelper.ShowIsCheck(dgvMajors);
            DataGridViewHelper.ApplyCheckedAllOrUncheckAll(dgvMajors, false);
            DataGridViewHelper.ChangeDgvHeaderText(dgvMajors, new[] { "IsChecked", "Code", "Name" },
                                                                   new[] { "Chọn", "Kí hiệu của ngành", "Ngành" });

            MajorCurrentRowChanged();
        }

        private void MajorCurrentRowChanged()
        {
            if (dgvMajors.CurrentRow != null)
            {
                txtMajorName.Text = dgvMajors.CurrentRow.Cells["Name"].Value.ToString();
                txtMajorCode.Text = dgvMajors.CurrentRow.Cells["Code"].Value.ToString();
            }

            else
                txtMajorName.Text = txtMajorCode.Text = string.Empty;
        }

        private void DgvMajorsCurrentCellChanged(object sender, EventArgs e)
        {
            MajorCurrentRowChanged();
        }

        private void DgvMajorsCellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (dgvMajors == null ||
                 dgvMajors.Columns[PropertiesConstants.IsChecked] == null ||
                 e.ColumnIndex != dgvMajors.Columns[PropertiesConstants.IsChecked].Index)
                return;

            dgvMajors[e.ColumnIndex, e.RowIndex].Value =
                !(bool)dgvMajors[e.ColumnIndex, e.RowIndex].Value;
          
            ckbAllMajors.Checked = DataGridViewHelper.AllRowsIsCheck(dgvMajors);
            ckbAllMajors.Text = string.Format((ckbAllMajors.Checked ? "Hủy" : "Lựa") + " chọn tất cả");
        }

        private void CkbAllMajorsCheckedChanged(object sender, EventArgs e)
        {
            DataGridViewHelper.ApplyCheckedAllOrUncheckAll(dgvMajors, ckbAllMajors.Checked);
        }

        private void ReloadMajor(Major major)
        {
            LoadMajor();
            DataGridViewHelper.SetDgvCurrentCell(dgvMajors, major.Name);
            MajorCurrentRowChanged();
        }

        /// <summary>
        /// Event click button Add Major
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void BtnAddMajorClick(object sender, EventArgs e)
        {
            try
            {
                var major = new Major
                {
                    Name = txtMajorName.Text.Trim(),
                    Code = txtMajorCode.Text.Trim()
                };

                string message;

                if (_business.AddMajorValidator(major, out message))
                {
                    _business.AddMajor(major);

                    ReloadMajor(major);
                }
                else
                    MessageBox.Show(message);

            }
            catch (Exception ex)
            {
                Logger.Write(null, ex);
            }
        }

        /// <summary>
        /// Event click edit Major
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void BtnEditMajorClick(object sender, EventArgs e)
        {
            try
            {
                if (dgvMajors.CurrentRow != null)
                {                    
                    var major = new Major
                                    {
                                        Name = txtMajorName.Text.Trim(),
                                        Code = txtMajorCode.Text.Trim(),
                                        Id = Convert.ToInt32(dgvMajors.CurrentRow.Cells[PropertiesConstants.Id].Value)
                                    };
                    string message;

                    if (_business.EditMajorValidator(major, out message))
                    {
                        if (!_business.EditMajor(major))
                        {
                            MessageBox.Show(ConstantMessages.SaveItemNotSuccess, FileConstants.ProgramName);
                            return;
                        }
                        ReloadMajor(major);
                    }
                    else
                        MessageBox.Show(message);
                }
                else
                    MessageBox.Show(ConstantMessages.NoSelected, FileConstants.ProgramName);
            }

            catch (Exception ex)
            {
                Logger.Write(null, ex);
            }
        }

        /// <summary>
        /// Event click Delete Major
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void BtnDeleteMajorClick(object sender, EventArgs e)
        {
            try
            {
                var majors = new List<Major>();
                string message;

                for (var i = 0; i < dgvMajors.RowCount; i++)
                {
                    if (!(bool)dgvMajors[(string)typeof(Major).Properties().IsChecked, i].Value) continue;

                    majors.Add(new Major
                    {
                        Id = (int)dgvMajors[(string)typeof(Major).Properties().Id, i].Value,
                        Name = dgvMajors[(string)typeof(Major).Properties().Name, i].Value.ToString(),
                        Code = dgvMajors["Code", i].Value.ToString(),
                    });
                }
                if (majors.Count < 1)
                {
                    MessageBox.Show(ConstantMessages.NoSelected, FileConstants.ProgramName);
                    return;
                }

                if (_business.DeleteMajorsValidator(majors, out message))
                {
                    if (MessageBox.Show(string.Format(ConstantMessages.ConfirmDeleteItem, " các ngành đã chọn"), FileConstants.ProgramName, MessageBoxButtons.YesNo) == DialogResult.Yes)
                    {
                        _business.DeleteMajors(majors);
                        ReloadMajor(null);
                    }
                }
                else MessageBox.Show(message);
            }
            catch (Exception ex)
            {
                Logger.Write(null, ex);
            }
        }

        #endregion

        #region -- Kind Intern --

        /// <summary>
        /// 
        /// </summary>
        private void LoadKindIntern()
        {
            dgvKindInterns.DataSource = _business.GetListKindIntern();

            DataGridViewHelper.ShowHideColumns(dgvKindInterns,
                                               new[] { PropertiesConstants.IsChecked, PropertiesConstants.Name, "Year" });
            DataGridViewHelper.ShowIsCheck(dgvKindInterns);
            DataGridViewHelper.ApplyCheckedAllOrUncheckAll(dgvKindInterns, false);
            DataGridViewHelper.ChangeDgvHeaderText(dgvKindInterns, new[] { "IsChecked", "Year", "Name" },
                                                                   new[] { "Chọn", "Năm", "Loại" });

            KindInternCurrentRowChanged();
        }

        private void KindInternCurrentRowChanged()
        {
            if (dgvKindInterns.CurrentRow != null)
            {
                txtKindIntern.Text = dgvKindInterns.CurrentRow.Cells["Name"].Value.ToString();
                txtYear.Text = dgvKindInterns.CurrentRow.Cells["Year"].Value.ToString();
            }

            else
                txtKindIntern.Text = txtYear.Text = string.Empty;
        }

        private void DgvKindInternsCurrentCellChanged(object sender, EventArgs e)
        {
            KindInternCurrentRowChanged();
        }

        private void DgvKindInternsCellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (dgvKindInterns == null ||
                 dgvKindInterns.Columns[PropertiesConstants.IsChecked] == null ||
                 e.ColumnIndex != dgvKindInterns.Columns[PropertiesConstants.IsChecked].Index)
                return;

            dgvKindInterns[e.ColumnIndex, e.RowIndex].Value =
                !(bool)dgvKindInterns[e.ColumnIndex, e.RowIndex].Value;

            ckbAllKindInterns.Checked = DataGridViewHelper.AllRowsIsCheck(dgvKindInterns);
            ckbAllKindInterns.Text = string.Format((ckbAllKindInterns.Checked ? "Hủy" : "Lựa") + " chọn tất cả");
        }

        private void CkbAllKindInternsCheckedChanged(object sender, EventArgs e)
        {
            DataGridViewHelper.ApplyCheckedAllOrUncheckAll(dgvKindInterns, ckbAllKindInterns.Checked);
        }

        private void ReloadKindIntern(KindIntern kindIntern)
        {
            LoadKindIntern();
            DataGridViewHelper.SetDgvCurrentCell(dgvKindInterns, kindIntern.Name);
            KindInternCurrentRowChanged();
        }
      
        /// <summary>
        /// Event click edit Kind Intern
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void BtnEditKindInternClick(object sender, EventArgs e)
        {
            try
            {
                if (dgvKindInterns.CurrentRow != null)
                {
                    var kindinter = new KindIntern
                    {
                        Name = txtKindIntern.Text.Trim(),
                        Year = Convert.ToInt32(txtYear.Text.Trim()),
                        Id = Convert.ToInt32(dgvKindInterns.CurrentRow.Cells[PropertiesConstants.Id].Value)
                    };
                    string message;

                    if (_business.EditKindInterValidator(kindinter, out message))
                    {
                        if (!_business.EditKindIntern(kindinter))
                        {
                            MessageBox.Show(ConstantMessages.SaveItemNotSuccess, FileConstants.ProgramName);
                            return;
                        }
                        ReloadKindIntern(kindinter);
                    }
                    else
                        MessageBox.Show(message);
                }
                else
                    MessageBox.Show(ConstantMessages.NoSelected, FileConstants.ProgramName);
            }

            catch (Exception ex)
            {
                Logger.Write(null, ex);
            }
        }
        #endregion

        #region -- Kind Mark --

        /// <summary>
        /// 
        /// </summary>
        private void LoadKindMark()
        {
            dgvKindMarks.DataSource = _business.GetListKindMark();

            DataGridViewHelper.ShowHideColumns(dgvKindMarks,
                                               new[] { PropertiesConstants.IsChecked, PropertiesConstants.Name});
            DataGridViewHelper.ShowIsCheck(dgvKindMarks);
            DataGridViewHelper.ApplyCheckedAllOrUncheckAll(dgvKindMarks, false);
            DataGridViewHelper.ChangeDgvHeaderText(dgvKindMarks, new[] { "IsChecked", "Name" },
                                                                   new[] { "Chọn", "Loại điểm số" });

            KindMarkCurrentRowChanged();
        }

        private void KindMarkCurrentRowChanged()
        {
            txtKindMark.Text = dgvKindMarks.CurrentRow != null ? dgvKindMarks.CurrentRow.Cells["Name"].Value.ToString() : string.Empty;
        }

        private void DgvKindMarksCurrentCellChanged(object sender, EventArgs e)
        {
            KindMarkCurrentRowChanged();
        }

        private void DgvKindMarksCellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (dgvKindMarks == null ||
                 dgvKindMarks.Columns[PropertiesConstants.IsChecked] == null ||
                 e.ColumnIndex != dgvKindMarks.Columns[PropertiesConstants.IsChecked].Index)
                return;

            dgvKindMarks[e.ColumnIndex, e.RowIndex].Value =
                !(bool)dgvKindMarks[e.ColumnIndex, e.RowIndex].Value;

            ckbAllKindMarks.Checked = DataGridViewHelper.AllRowsIsCheck(dgvKindMarks);
            ckbAllKindMarks.Text = string.Format((ckbAllKindMarks.Checked ? "Hủy" : "Lựa") + " chọn tất cả");
        }

        private void CkbAllKindMarksCheckedChanged(object sender, EventArgs e)
        {
            DataGridViewHelper.ApplyCheckedAllOrUncheckAll(dgvKindMarks, ckbAllKindMarks.Checked);
        }

        private void ReloadKindMark(KindMark kindMark)
        {
            LoadKindMark();
            DataGridViewHelper.SetDgvCurrentCell(dgvKindMarks, kindMark.Name);
            KindMarkCurrentRowChanged();
        }

        /// <summary>
        /// Event click edit Kind Mark
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void BtnEditKindMarkClick(object sender, EventArgs e)
        {
            try
            {
                if (dgvKindMarks.CurrentRow != null)
                {
                    var kindmark = new KindMark
                    {
                        Name = txtKindMark.Text.Trim(),
                        Id = Convert.ToInt32(dgvKindMarks.CurrentRow.Cells[PropertiesConstants.Id].Value)
                    };
                    string message;

                    if (_business.EditKindMarkValidator(kindmark, out message))
                    {
                        if (!_business.EditKindMark(kindmark))
                        {
                            MessageBox.Show(ConstantMessages.SaveItemNotSuccess, FileConstants.ProgramName);
                            return;
                        }
                        ReloadKindMark(kindmark);
                    }
                    else
                        MessageBox.Show(message);
                }
                else
                    MessageBox.Show(ConstantMessages.NoSelected, FileConstants.ProgramName);
            }

            catch (Exception ex)
            {
                Logger.Write(null, ex);
            }
        }
        #endregion
    }
}
